NextJs / Web / Laravel with reactjs web
Laravel and Reactjs Web
-
STEPS
Step 1 : Install Configuration For ReactJS
php artisan preset react npm install npm run dev dependency
npm install react-router@2.8.1 Step 2 : Create React Components Files
1) app.js
2) bootstrap.js
3) components/CreateProduct.js
4) components/DisplayProduct.js
5) components/MasterProduct.js
6) components/MyGlobleSetting.js
7) components/TableRow.js
8) components/UpdateProduct.jsresources/assets/js/app.js
require('./bootstrap'); import React from 'react'; import { render } from 'react-dom'; import { Router, Route, browserHistory } from 'react-router'; import Master from './components/Master'; import CreateProduct from './components/CreateProduct'; import DisplayProduct from './components/DisplayProduct'; import UpdateProduct from './components/UpdateProduct'; render( resources/assets/js/bootstrap.js
window._ = require('lodash'); try { window.$ = window.jQuery = require('jquery'); require('bootstrap-sass'); } catch (e) {} window.axios = require('axios'); window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; resources/assets/js/components/CreateProduct.js
import React, {Component} from 'react'; import {browserHistory} from 'react-router'; import MyGlobleSetting from './MyGlobleSetting'; class CreateProduct extends Component { constructor(props){ super(props); this.state = {productTitle: '', productBody: ''}; this.handleChange1 = this.handleChange1.bind(this); this.handleChange2 = this.handleChange2.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } handleChange1(e){ this.setState({ productTitle: e.target.value }) } handleChange2(e){ this.setState({ productBody: e.target.value }) } handleSubmit(e){ e.preventDefault(); const products = { title: this.state.productTitle, body: this.state.productBody } let uri = MyGlobleSetting.url + '/api/products'; axios.post(uri, products).then((response) => { browserHistory.push('/display-item'); }); } render() { return ( Create Product
resources/assets/js/components/DisplayProduct.js
import React, {Component} from 'react'; import axios from 'axios'; import { Link } from 'react-router'; import TableRow from './TableRow'; import MyGlobleSetting from './MyGlobleSetting'; class DisplayProduct extends Component { constructor(props) { super(props); this.state = {value: '', products: ''}; } componentDidMount(){ axios.get(MyGlobleSetting.url + '/api/products') .then(response => { this.setState({ products: response.data }); }) .catch(function (error) { console.log(error); }) } tabRow(){ if(this.state.products instanceof Array){ return this.state.products.map(function(object, i){ return ; }) } } render(){ return ( Products
Create Product
ID Product Title Product Body Actions resources/assets/js/components/Master.js
import React, {Component} from 'react'; import { Router, Route, Link } from 'react-router'; class Master extends Component { render(){ return ( {this.props.children}resources/assets/js/components/MyGlobleSetting.js
class MyGlobleSetting { constructor() { this.url = 'http://localhost:8000'; } } export default (new MyGlobleSetting); resources/assets/js/components/TableRow.js
import React, { Component } from 'react'; import { Link, browserHistory } from 'react-router'; import MyGlobleSetting from './MyGlobleSetting'; class TableRow extends Component { constructor(props) { super(props); this.handleSubmit = this.handleSubmit.bind(this); } handleSubmit(event) { event.preventDefault(); let uri = MyGlobleSetting.url + `/api/products/${this.props.obj.id}`; axios.delete(uri); browserHistory.push('/display-item'); } render() { return ( {this.props.obj.id} {this.props.obj.title} {this.props.obj.body} resources/assets/js/components/UpdateProduct.js
import React, {Component} from 'react'; import axios from 'axios'; import { Link } from 'react-router'; import MyGlobleSetting from './MyGlobleSetting'; class UpdateProduct extends Component { constructor(props) { super(props); this.state = {title: '', body: ''}; this.handleChange1 = this.handleChange1.bind(this); this.handleChange2 = this.handleChange2.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } componentDidMount(){ axios.get(MyGlobleSetting.url + `/api/products/${this.props.params.id}/edit`) .then(response => { this.setState({ title: response.data.title, body: response.data.body }); }) .catch(function (error) { console.log(error); }) } handleChange1(e){ this.setState({ title: e.target.value }) } handleChange2(e){ this.setState({ body: e.target.value }) } handleSubmit(event) { event.preventDefault(); const products = { title: this.state.title, body: this.state.body } let uri = MyGlobleSetting.url + '/api/products/'+this.props.params.id; axios.patch(uri, products).then((response) => { this.props.history.push('/display-item'); }); } render(){ return ( Update Product
Return to ProductStep 3 : Create Main Blade File
resources/views/welcome.blade.phpLaravel 5.5 ReactJS CRUD Example Step 4 : Run Project
npm run dev Now you can open bellow URL on your browser:
http://localhost:8000/